//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2007
//
// 'Options' dialog, 'Edit' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "Options.h"
# include "Settings.h"
# include "MapView.h"
# include "PropertyPages.h"
# include "Types.h"
# include "keyboard.h"
# include "SelectColor.h"
# include "Button.h"
# include "Globals.h"

INT_PTR CALLBACK _OptionsEditProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	switch (_uMsg) {
		case WM_INITDIALOG: {
			CenterPropertyPages (_hWnd);

			// Snap to grid.
			::CheckDlgButton (_hWnd, IDC_ENABLE_GRID,  GetSetting ("OptionSnapToGrid", true) ? BST_CHECKED : BST_UNCHECKED);

			// Stick to neighbours.
			::CheckDlgButton (_hWnd, IDC_ENABLE_STICK, GetSetting ("OptionStick",      true) ? BST_CHECKED : BST_UNCHECKED);

			// New object color & width.
			ListView_SetBkColor (::GetDlgItem (_hWnd, IDC_COLOR_NEW), GetNewObjectColor ());
			::SetDlgItemInt (_hWnd, IDC_WIDTH_NEW, GetNewObjectWidth (), FALSE);

			// Default type set.
			const HWND hTypeSet = ::GetDlgItem (_hWnd, IDC_TYPESET);
			const CTypeSet * const pDefaultTypeSet = GetDefaultTypeSet ();
			size_t cTypeSet = 0;
			for (const CTypeSet * pTypeSet = CTypeSet::s_pFirst; pTypeSet; pTypeSet = pTypeSet->m_pNext) {
				if (pTypeSet == & g_tsNull)
					continue;

				::SendMessage (hTypeSet, CB_INSERTSTRING, cTypeSet, reinterpret_cast<LPARAM> (pTypeSet->strDisplayName));
				::SendMessage (hTypeSet, CB_SETITEMDATA,  cTypeSet, reinterpret_cast<LPARAM> (pTypeSet));

				if (pTypeSet == pDefaultTypeSet)
					::SendMessage (hTypeSet, CB_SETCURSEL, cTypeSet, 0);

				++ cTypeSet;
			}

			// Buttons for multiple selection.
			const HWND hMSel = ::GetDlgItem (_hWnd, IDC_MSEL);
			::SendMessage (hMSel, CB_INSERTSTRING, 0, reinterpret_cast<LPARAM> ("SHIFT for multiple selection, CTRL for elements selection"));
			::SendMessage (hMSel, CB_INSERTSTRING, 1, reinterpret_cast<LPARAM> ("CTRL for multiple selection, SHIFT for elements selection"));
			::SendMessage (hMSel, CB_SETCURSEL, g_bMSelByShift ? 0 : 1, 0);

			return TRUE;
		}

		case WM_COMMAND: {
			switch (LOWORD (_wParam)) {
				case IDC_ENABLE_GRID:
				case IDC_ENABLE_STICK:
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					return TRUE;

				case IDC_TYPESET:
				case IDC_MSEL:
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					return TRUE;

				case IDC_SELECT_COLOR_NEW: {
					const HWND hColor = ::GetDlgItem (_hWnd, IDC_COLOR_NEW);
					COLORREF cr = ListView_GetBkColor (hColor);
					if (SelectColor (_hWnd, cr)) {
						ListView_SetBkColor (hColor, cr);
						::InvalidateRect (hColor, NULL, TRUE);
						::UpdateWindow (hColor);

						PropSheet_Changed (::GetParent (_hWnd), _hWnd);
					}

					return TRUE;
				}

				case IDC_WIDTH_NEW:
					switch (HIWORD (_wParam)) {
						case EN_CHANGE:
							PropSheet_Changed (::GetParent (_hWnd), _hWnd);
							return TRUE;
					}
					break;

				case IDC_KEYBOARD:
					CustomizeKeyboard (_hWnd);
					break;
			}
			break;
		}

		case WM_NOTIFY: {
            const NMHDR * const pInfo = reinterpret_cast<NMHDR *> (_lParam);
            switch (pInfo->code) {
				case PSN_APPLY: {
					g_bEnableSnapToGrid = ::IsDlgButtonChecked (_hWnd, IDC_ENABLE_GRID)  == BST_CHECKED;
					g_bEnableSticking   = ::IsDlgButtonChecked (_hWnd, IDC_ENABLE_STICK) == BST_CHECKED;

					SaveSetting ("OptionSnapToGrid", g_bEnableSnapToGrid);
					SaveSetting ("OptionStick",      g_bEnableSticking);

					// Color.
					SaveSetting ("Color_New", ListView_GetBkColor (::GetDlgItem (_hWnd, IDC_COLOR_NEW)));

					// Width.
					size_t cWidth = ::GetDlgItemInt (_hWnd, IDC_WIDTH_NEW, NULL, FALSE);
					if (cWidth == 0)
						cWidth = 1;
					if (cWidth > 10)
						cWidth = 10;
					SaveSetting ("Width_New", (DWORD) cWidth);

					// Default type set.
					{
						const HWND hTypeSet = ::GetDlgItem (_hWnd, IDC_TYPESET);
						const size_t cSelIndex = ::SendMessage (hTypeSet, CB_GETCURSEL, 0, 0);
						const CTypeSet * const pTypeSet = reinterpret_cast<const CTypeSet *> (::SendMessage (hTypeSet, CB_GETITEMDATA, cSelIndex, 0));
						SaveSetting ("DefaultTypeSet", pTypeSet->strID);
					}

					Refresh ();

					// Buttons for multiple selection.
					{
						const HWND hMSel = ::GetDlgItem (_hWnd, IDC_MSEL);
						const size_t cSelIndex = ::SendMessage (hMSel, CB_GETCURSEL, 0, 0);
						g_bMSelByShift = cSelIndex == 0;
						SaveSetting ("MSelByShift", g_bMSelByShift);
					}

					break;
				}
			}
			break;
		}
	}

	return FALSE;
}

COLORREF GetNewObjectColor () {
	return GetSetting ("Color_New", (DWORD) c_colGray);
}

size_t GetNewObjectWidth () {
	return GetSetting ("Width_New", (DWORD) 1);
}

const CTypeSet * GetDefaultTypeSet () {
	char strTypeSet [32];
	GetSetting ("DefaultTypeSet", strTypeSet, sizeof (strTypeSet), "Garmin");
	return CTypeSet::FindTypeSet (strTypeSet);
}
